/**
 * 
 */
package game.kill.biz.impl;

import game.kill.biz.GameBiz;
import game.kill.biz.GameRuleConfig;
import game.kill.biz.MessageSendBiz;
import game.kill.enums.GameStepEnum;
import game.kill.enums.PlayerActionEnum;
import game.kill.enums.PlayerRoleEnum;
import game.kill.enums.PlayerStatusEnum;
import game.kill.exception.KillException;
import game.kill.exception.KillException.KillErrorCode;
import game.kill.model.GameRoomInfo;
import game.kill.model.PlayerAction;
import game.kill.model.RoomUser;
import game.kill.model.StepActionRecord;
import game.kill.service.StepActionRecordService;
import game.kill.util.GameRuleUtils;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * @author leiruiqi
 *
 */

@Service("gameBiz")
public class GameBizImpl implements GameBiz {

	@Resource
	private StepActionRecordService stepActionService;
	
	@Resource
	private MessageSendBiz messageSendBiz;
	
	@Override
	public void gamePlayAction(PlayerAction playerAction, GameRoomInfo gameRoomInfo) {
		
		PlayerActionEnum action = playerAction.getActionEnum();
		
		PlayerRoleEnum role = playerAction.getRoleEnum();
		
		GameStepEnum step = GameStepEnum.getByName(gameRoomInfo.getRoom().getStep());
		
		if(!checkStepRoleAction(role,action,step)){
			throw new KillException(KillErrorCode.STATUE_ERROR,"操作非法");
		}
		if(!checkRuleAction(playerAction,gameRoomInfo)){
			throw new KillException(KillErrorCode.STATUE_ERROR,"操作非法");
		}
		
		StepActionRecord stepActionRecord = createActionRecord(playerAction.getRoomUser(),action,playerAction.getTargetInfo(),gameRoomInfo);
		
		doBizAfterCreateActionRecord(playerAction, stepActionRecord, gameRoomInfo);
		
	}
	
	private void doBizAfterCreateActionRecord(PlayerAction playerAction,StepActionRecord stepActionRecord,GameRoomInfo gameRoomInfo){
		messageSendBiz.sendMsgByPlayerAction(playerAction, stepActionRecord, gameRoomInfo);
		
		
	}
	
	
	private StepActionRecord createActionRecord(RoomUser roomUser,PlayerActionEnum playerAction,String targetInfo,GameRoomInfo gameRoomInfo){
		StepActionRecord stepAction = createStepAction4GameRoomInfo(gameRoomInfo);
		stepAction.setPlayerNumber(roomUser.getNumber());
		stepAction.setAction(playerAction.name());
		if(GameRuleUtils.isTargetAction(playerAction) ){
			stepAction.setTarget(Integer.valueOf(targetInfo));
		}else{
			stepAction.setInfo(targetInfo);
		}
			
		
		
		
		stepActionService.save(stepAction);
		return stepAction;
	}
	
	private StepActionRecord createStepAction4GameRoomInfo(GameRoomInfo gameRoomInfo){
		StepActionRecord stepAction = new StepActionRecord();
		stepAction.setRoomId(gameRoomInfo.getRoom().getId());
		stepAction.setRoundNum(gameRoomInfo.getRoom().getRound());
		stepAction.setStep(gameRoomInfo.getRoom().getStep());
		return stepAction;
		//stepAction.setPlayerNumber(gameRoomInfo.getRoomUserMap().get(key));
	}

	private boolean checkStepRoleAction(PlayerRoleEnum role,PlayerActionEnum action,GameStepEnum step){
		List<String> roleList = GameRuleConfig.stepRoleMap.get(step.name());
		
		List<String> actionList = GameRuleConfig.stepActionMap.get(step.name());
		
		if(CollectionUtils.isNotEmpty(roleList) && CollectionUtils.isNotEmpty(actionList)){
			return roleList.contains(role.name()) && actionList.contains(action.name());
		}
		return false;
	}
	
	private boolean checkRuleAction(PlayerAction playerAction,GameRoomInfo gameRoomInfo){
		
		PlayerActionEnum action = playerAction.getActionEnum();
		RoomUser roomUser = playerAction.getRoomUser();
		if(action == PlayerActionEnum.kill){
			if(roomUser.getStatus() == PlayerStatusEnum.normal.getCode()){
				String target = playerAction.getTargetInfo();
				if(StringUtils.isNumeric(target)){
					Integer playerNum = Integer.valueOf(target);
					if(gameRoomInfo.isAlivePeople(playerNum)){
						return true;
					}
				}
				
			}
			return false;
		}
		
		if(action == PlayerActionEnum.discuss){
			return true;
		}
		
		if(action == PlayerActionEnum.check){
			if(roomUser.getStatus() == PlayerStatusEnum.normal.getCode()){
				String target = playerAction.getTargetInfo();
				if(StringUtils.isNumeric(target)){
					Integer playerNum = Integer.valueOf(target);
					if(gameRoomInfo.isAlivePeople(playerNum)){
						return true;
					}
				}
				
			}
			return false;
		}
		
		if(action == PlayerActionEnum.speak){
			if(roomUser.getStatus() == PlayerStatusEnum.normal.getCode()){
				return true;
			}
			return false;
		}
		
		if(action == PlayerActionEnum.vote){
			String target = playerAction.getTargetInfo();
			if(StringUtils.isNumeric(target)){
				Integer playerNum = Integer.valueOf(target);
				if(gameRoomInfo.isAlivePeople(playerNum)){
					return true;
				}
			}
			return false;
		}
		
		return false;
	}
}
